package com.face.classic.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.face.classic.dto.WxLoginDTO;
import com.face.classic.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijie
 * @CreateTime: 2024-12-25  11:44
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@Service
public class WxLoginServiceImpl implements LoginService {
    /**
     * 微信appid
     */
    private static String APPID = "wxe393c357865443b2";
    /**
     * 微信secret
     */
    private static String SECRET = "5b1357013e8015c69ab7ab1f72518f3e";
    /**
     * 微信登录地址
     */
    private String REDIRECT_URL = "https://api.weixin.qq.com/sns/jscode2session";
    private static Map<String, Object> map = new HashMap<>();

    static {
        map.put("appid", APPID);
        map.put("secret", SECRET);
        map.put("grant_type", "authorization_code");
    }

    @Override
    public String login(String code) {
        map.put("js_code", code);
        String result = HttpUtil.get(REDIRECT_URL, map);
        WxLoginDTO wxLoginDTO = JSONUtil.toBean(result, WxLoginDTO.class);
        wxLoginDTO.getUserDetails();
        return result;
    }
}
